﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Serialization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using ProtoBuf;

namespace LanguageEditor
{
    public partial class Main : Form
    {
        private StringContainer stringContainer;

        public Main()
        {
            InitializeComponent();
        }

        private void Main_Load(object sender, EventArgs e)
        {
            stringContainer = new StringContainer();
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            OpenFileDialog importDialog = new OpenFileDialog();
            importDialog.Filter = "Language files|*.lng";
            if (importDialog.ShowDialog() == DialogResult.OK)
            {
                string path = importDialog.FileName;
                FileStream stream = File.Open(path, FileMode.OpenOrCreate);
                stringContainer = Serializer.Deserialize<StringContainer>(stream);
                stream.Close();
                lstKeys.Items.Clear();
                lstKeys.Items.AddRange(stringContainer.getAllKeys());
            }
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            SaveFileDialog exportDialog = new SaveFileDialog();
            exportDialog.DefaultExt = "lng";
            if (exportDialog.ShowDialog() == DialogResult.OK)
            {
                string Path = exportDialog.FileName;
                FileStream file = File.Create(Path);
                Serializer.Serialize(file, stringContainer);
                file.Close();
                
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            int index = lstKeys.Items.Add(txtString.Text);
            stringContainer.addValue("not set yet", txtString.Text);
            lstKeys.SelectedIndex = index;
        }

        private void lstKeys_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (((ListBox)sender).SelectedIndex > -1)
            {
                txtString.Text = stringContainer.getValue(((ListBox)sender).SelectedItem.ToString());
            }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            stringContainer.removeValue(lstKeys.SelectedItem.ToString());
            lstKeys.Items.RemoveAt(lstKeys.SelectedIndex);
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            stringContainer.addValue(txtString.Text, lstKeys.SelectedItem.ToString());
            txtString.Text = "";
        }
    }
}
